<?php

namespace app\mobile\controller;

class Line extends Common {

        public function index() {
                if (!$_REQUEST['id']) {
                        $this->_empty();
                } else {
                        $this->assign('id', $_REQUEST['id']);
                }
                $id = $_REQUEST['id'];
                $Tours = D("tours/Tours");
                //线路读取
                $this_line = $Tours->field(true)->relation(true)->cache()->find($_REQUEST['id']);
                if (empty($this_line)) {
                        return $this->_empty();
                }
                //面包屑
                $crumbs = array();
                $model = M("ToursTrip");
                $where = array();
                $where['tours_id'] = $id;
                $tirp = $model->cache()->where($where)->order("day asc")->select();
                foreach ($tirp as $key => $value) {
                        $tirp[$key]['images'] = array();
                        $images = unserialize($value['images']);
                        if (!empty($images)) {
                                if (is_array($images)) {
                                        foreach ($images as $k => $val) {
                                                $tirp[$key]['images'][] = array('url' => "javascript:;", 'src' => $val['savepath'] . $val['savename']);
                                        }
                                } else {
                                        $tirp[$key]['images'][0] = array('url' => "javascript:;", 'src' => $images[0]['savepath'] . $images[0]['savename']);
                                }
                        }
                }
                $this->assign('trip', $tirp);
                $this->assign('lineid', $id);


                $zone1 = M("Location")->field('id,name,write_string')->where(array('pid' => array('in', '1,2'), 'id' => array('in', $this_line['cid'])))->find();
                if ($zone1) {
                        $crumbs[] = array(
                            'name' => $zone1['name'],
                            'url' => U('/' . $zone1['write_string'])
                        );
                }
                $zone2 = '';
                //途经景点
                $this_line['sight_cache'] = str_replace('，', ',', $this_line['sight_cache']);
                $this_line['sight_t'] = explode(',', $this_line['sight_cache']);
                //热门景点


                $zone = $zone2 ? $zone2 : $zone1;
                $this->assign('line', $this_line);
                //相关线路
                $list = $Tours->order("id desc")->where(array('cid' => array('like', "%," . $zone['id'] . ",%"), 'id' => array('neq', $this_line['id']), 'isnotshow' => 0))->limit(5)->cache()->select();
                $this->assign('lines_list', $list);

                //在线咨询
                $counsel_list = D("ToursCounsel")->where(array('tours_id' => $this_line['id'], 'reply' => array('neq', '')))->order('create_time desc')->limit(6)->select();
                $this->assign('counsel_list', $counsel_list);

                //SEO优化，生成TITLE,K,D
                $from = array('，', '{linename}', '{lineto}', '{linesub}');
                $to = array(',', $this_line['title'], $zone['name'], $this_line['subtitle']);
                $seo_title = $this_line['seo_title'] ? $this_line['seo_title'] : str_replace($from, $to, C('site.line_seotitle'));
                $seo_keywords = $this_line['seo_keywords'] ? $this_line['seo_keywords'] : str_replace($from, $to, C('site.line_seokeywords'));
                $seo_description = $this_line['seo_description'] ? $this_line['seo_description'] : str_replace($from, $to, C('site.line_seodescription'));
                $this->assign('seo_title', $seo_title);
                $this->assign('seo_keywords', $seo_keywords);
                $this->assign('seo_description', $seo_description);
                return $this->fetch();
        }

        public function book() {
                $id = I('get.id');

                //线路读取
                $tours = D("tours/Tours");
                $this_line = $tours->relation(true)->field(true)->find($id);
                $this->assign('line', $this_line);


                return $this->fetch();
        }

        /*
         * 切换价格
         */

        protected $show_week_ch = array('日', '一', '二', '三', '四', '五', '六');

        public function PriceCheck() {
                if (IS_AJAX) {
                        $price_id = I('post.price_id');
                        $tours_id = I('post.tours_id');
                        $result['tours_price'] = M('ToursPrice')->where(array('tours_id' => $tours_id))->cache()->select();
                        $leixin = M('ToursPrice')->where(array('id' => $price_id))->cache()->find();
                        $result['delay_time'] = array();

                        $this_year = date("Y");
                        $this_month = date("m");
                        $this_day = date("d");
                        $tn = 1;
                        for ($i = 1; $i <= 160; $i++) {
                                if ($tn > 30)
                                        break;
                                $show_day = date("Y-m-d", mktime($i * 24, 0, 0, $this_month, $this_day, $this_year));
                                $show_week = date("w", mktime($i * 24, 0, 0, $this_month, $this_day, $this_year));
                                $show_tian = date("d", mktime($i * 24, 0, 0, $this_month, $this_day, $this_year));
                                $show_week_ch = $this->show_week_ch[$show_week];
                                $show_day_time = strtotime($show_day);
                                if (!empty($result['tours_price'])) {
                                        $result['delay_time']['sprice'] = $leixin['sprice'];
                                        foreach ($result['tours_price'] as $key => $value) {
                                                //显示标签相同的第一个标签下的价格汇总
                                                if ($value['leixin'] == $leixin['leixin'] && $value['case_id'] == $leixin['case_id']) {
                                                        $value['price_value'] = unserialize($value['price_value']);
                                                        switch ($value['price_type']) {
                                                                case 1:
                                                                        if ($show_day_time >= strtotime($value['start_time']) && ($show_day_time <= strtotime($value['end_time']) || empty($value['start_time']))) {
                                                                                if ($value['price_value']['adult'] > 0) {
                                                                                        $result['delay_time'][$show_day] = $show_day . " （周" . $show_week_ch . "） " . $value['price_value']['adult'] . "元/人";

                                                                                        if ($value['price_value']['child'] > 0) {
                                                                                                $result['delay_time'][$show_day] .= " " . $value['price_value']['child'] . "元/儿童";
                                                                                        }

                                                                                        $tn++;
                                                                                        break 2;
                                                                                }
                                                                        }
                                                                        break;
                                                                case 2:
                                                                        if ($show_day_time >= strtotime($value['start_time']) && ($show_day_time <= strtotime($value['end_time']) || empty($value['start_time']))) {
                                                                                $value['price_value'] = array_values($value['price_value']);
                                                                                $adult_index = ($show_week - 1) < 0 ? 6 : ($show_week - 1);
                                                                                $child_index = ($show_week + 13 ) < 14 ? 20 : ($show_week + 13);
                                                                                if ($value['price_value'][$adult_index] > 0) {
                                                                                        $result['delay_time'][$show_day] = $show_day . " （周" . $show_week_ch . "） " . $value['price_value'][$adult_index] . "元/人";
                                                                                        if ($value['price_value'][$child_index] > 0) {
                                                                                                $result['delay_time'][$show_day] .= " " . $value['price_value'][$child_index] . "元/儿童";
                                                                                        }
                                                                                        $tn++;
                                                                                        break 2;
                                                                                }
                                                                        }
                                                                        break;
                                                                case 3:
                                                                        if ($show_day_time >= strtotime($value['start_time']) && ($show_day_time <= strtotime($value['end_time']) || empty($value['start_time']))) {
                                                                                foreach ($value['price_value']['day'] as $k => $val) {
                                                                                        if ($val == $show_tian && $value['price_value']['adult'][$k] > 0) {
                                                                                                $result['delay_time'][$show_day] = $show_day . " （周" . $show_week_ch . "） " . $value['price_value']['adult'][$k] . "元/人";
                                                                                                if ($value['price_value']['child'][$k] > 0) {
                                                                                                        $result['delay_time'][$show_day] .= " " . $value['price_value']['child'][$k] . "元/儿童";
                                                                                                }
                                                                                                $tn++;
                                                                                                break 3;
                                                                                        }
                                                                                }
                                                                        }
                                                                        break;
                                                                case 4:
                                                                        if ($show_day_time == strtotime($value['start_time'])) {
                                                                                if ($value['price_value']['adult'] > 0) {
                                                                                        $result['delay_time'][$show_day] = $show_day . " （周" . $show_week_ch . "） " . $value['price_value']['adult'] . "元/人";
                                                                                        if ($value['price_value']['child'] > 0) {
                                                                                                $result['delay_time'][$show_day] .= " " . $value['price_value']['child'] . "元/儿童";
                                                                                        }
                                                                                        $tn++;
                                                                                        break 2;
                                                                                }
                                                                        }
                                                                        break;
                                                        }
                                                }
                                        }
                                }
                        }
                        if (empty($result['delay_time']) || count($result['delay_time']) == 1) {
                                $result['delay_time'][0] = '固定日期发团';
                                $this->ajaxReturn($result['delay_time'], 'JSON');
                        } else {
                                $this->ajaxReturn($result['delay_time'], 'JSON');
                                exit();
                        }
                }
        }

        public function mobiledan() {
                if (!IS_AJAX) {
                        return $this->error("页面不存在");
                }
                $data = array(
                    'line_id' => I('line_id'),
                    'supplier_id' => I('supplier_id'),
                    'line_name' => I('line_name'),
                    'line_phone' => I('line_phone'),
                    'line_ren' => I('line_ren'),
                    'status' => 0,
                    'ip' => get_client_ip(),
                    'line_time' => time()
                );
                $model = M("mobile_order");
                if ($model->data($data)->add()) {
                        $this->ajaxReturn($data['status'], '操作成功后', 1, 'json');
                }
        }

}
